﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SwinGame;

namespace SimpleSurvival.src
{
    //-----------------------------------------------//
    //CLASS - Map.cs
    //-----------------------------------------------//
    //Contains all the game objects.
    //Next iteration - add quadtree support.
    //http://gamedev.tutsplus.com/tutorials/implementation/quick-tip-use-quadtrees-to-detect-likely-collisions-in-2d-space/
    public class Map
    {
        List<IEntity> _entitys = new List<IEntity>();
        List<IUnit> _units = new List<IUnit>();
        List<ITerrainTile> _terrain = new List<ITerrainTile>(); //Next iteration - convert to a 2d Array

        public List<ITerrainTile> Terrain { get { return _terrain; } }
        public List<IUnit> Units { get { return _units; } }
        public List<IEntity> Entitys { get { return _entitys; } }

        //Updates every entity in the map
        public void UpdateAllEntitys(double dt)
        {
            IUnit deadUnit = null;
            bool bFoundDeadUnit = false;

            foreach (IUnit u in _units)
            {
                if (u.Hp < 0)
                {
                    bFoundDeadUnit = true;
                    deadUnit = u;
                }
            }

            if (bFoundDeadUnit) //removes one dead unit each frame - fix next iter
            {
                RemoveEntity(deadUnit.Entity);
            }

            for (int j = 0; j < _entitys.Count; j++ )
            {
                _entitys[j].Update(dt);
            }
        }

        //Add a new entity to the map
        public void EntityCreatedWithinMap(IEntity e)
        {
            _entitys.Add(e);    //Add to entity list
            IUnit u = e as IUnit; if (u != null) { _units.Add(u); } //If is Init - Add to IUnit list as well
            else
            {
                //if is ITerrainTile add to ITerrainTile list instead
                ITerrainTile tt = e as ITerrainTile; if (tt != null) { _terrain.Add(tt); }  
            }
        }
        
        //Remove entity from the map
        public void RemoveEntity(IEntity e) 
        {
            _entitys.Remove(e);
            IUnit u = e as IUnit; if (u != null) { _units.Remove(u); }
            else
            {
                ITerrainTile tt = e as ITerrainTile; if (tt != null) { _terrain.Remove(tt); }
            }
        }

        //Create the terrain for the map
        void LoadTerrain(EntityFactory ef)
        {
            for (int i = 0; i < 40; i++)for (int j = 0; j < 40; j++)
            {
                ef.MakeTerrainTile(j, i, "DIRT");
            }
        }

        void LoadUnits(EntityFactory ef)
        {
            ef.MakeUnit("HUMAN", new Vector { X = 300, Y = 200 });  //Move into Load Units
            //ef.MakeUnit("SLIME", new Vector { X = 600, Y = 400 });
        }

        public void PopulateMap(EntityFactory ef)
        {
            LoadTerrain(ef);
            LoadUnits(ef);
        }

        public Map() { }

    }
}
